package com.wanxi.service;

import com.wanxi.dao.BrandDao;
import com.wanxi.model.BrandModel;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

public class BrandService {


    /**
     * 增加品牌
     *
     * @param model
     * @return
     */
    public String add(BrandModel model) {
        model.setId(BrandDao.brandModelList.size() + 1);
        BrandDao.brandModelList.add(model);
        return "success";
    }

    /**
     * 查询品牌
     *
     * @return
     */
    public List<BrandModel> findAll() {
        return BrandDao.brandModelList;
    }

    public BrandModel findById(BrandModel model) {
        for (BrandModel m : BrandDao.brandModelList
        ) {
            if (Objects.equals(m.getId(), model.getId())) {
                return m;
            }
        }
        return null;
    }

    public String update(BrandModel model) {
//        Iterator<BrandModel> iterator = BrandDao.brandModelList.iterator();
//        while (iterator.hasNext()) {
//            if (iterator.next().getId().equals(model.getId())) {
//                iterator.remove();
//                BrandDao.brandModelList.add(model);
//                return "";
//            }
//        }
        for (int i = 0; i < BrandDao.brandModelList.size(); i++) {
            if (Objects.equals(model.getId(), BrandDao.brandModelList.get(i).getId())) {
                BrandDao.brandModelList.set(i, model);
                return "";
            }
        }
        return "";
    }

    public String delete(BrandModel model) {
        for (int i = 0; i < BrandDao.brandModelList.size(); i++) {
            if (Objects.equals(model.getId(), BrandDao.brandModelList.get(i).getId())) {
                BrandDao.brandModelList.remove(i);
                return "";
            }
        }
        return "";
    }
}
